﻿using System;
using Utils.Wpf;
using Visiblox.Charts;

namespace StockAnalyst2.Chart
{
	internal class BandSeriesVM : ViewModelBase, IDisposable
	{
		#region Attributes

		private readonly BandSeries _model;
		private readonly BandModelToVisibloxConverter _converter;

		#endregion

		#region Properties

		public DataSeries<DateTime, decimal> Data { get; private set; }
		public FilteredDataSeries FilteredData { get; private set; }

		#endregion

		#region Public Methods

		public BandSeriesVM(BandSeries model, string title)
		{
			_model = model;

			this.Data = new DataSeries<DateTime, decimal>(title);
			this.FilteredData = new FilteredDataSeries();

			this.FilteredData.OriginalDataSeries = this.Data;
			this.FilteredData.Title = title;
			this.FilteredData.FilterStrategy = new WindowedFilterStrategy();

			_converter = new BandModelToVisibloxConverter(_model.Data, this.Data, base.Dispatcher);
		}

		#endregion

		#region IDisposable

		protected bool Disposed { get; private set; }

		private void Dispose(bool disposing)
		{
			if (!this.Disposed)
			{
				this.InternalDispose(disposing);
			}

			this.Disposed = true;
		}

		protected virtual void InternalDispose(bool disposing)
		{
			if (disposing)
			{
				_converter.Dispose();
			}

			// Dispose here only unmanaged objects 
			// Don’t use managed objects here because maybe 
			// they have been finalized already
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		~BandSeriesVM()
		{
			this.Dispose(false);
		}

		#endregion
	}
}
